#include <stdio.h>

#define DAYS_IN_YEAR 365
#define PI 3.14159

/**
 * 格式化输出
 */
int main(void) {

	/*
	 * 常量使用 define 定义，与预处理指令写在同一行，末尾一定不要使用分号
	 */

	// 收入
	float income = 1234567890.12F;

	// 整数转换规范
	printf("CONVERSION SPECIFICATIONS FOR INTEGERS \n\n");
	printf("Days in year = \n"
		   // 字段宽度小于实际
		   "[[%1d]] \t(field width less than actual)\n"
		   // 字段宽度大于实际
		   "[[%9d]] \t(field width greater than actual)\n"
		   // 未指定字段宽度
		   "[[%d]] \t(no field width specified) \n\n\n",
		   DAYS_IN_YEAR, DAYS_IN_YEAR, DAYS_IN_YEAR);

	// 实数转换规范
	printf("CONVERSION SPECIFICATIONS FOR REAL NUMBERS \n\n");
	// 正确指定精度的情况
	printf("Cases for precision being specified correctly \n");
	printf("PI = \n"
		   // 字段宽度小于实际
		   "[[%1.5f]] \t\t(field width less than actual) \n"
		   // 字段宽度大于实际
		   "[[%15.5f]] \t(field width greater than actual) \n"
		   // 未指定字段宽度
		   "[[%.5f]] \t\t(no field width specified) \n\n",
		   PI, PI, PI);

	// 正确指定字段宽度的情况
	printf("Cases for field width being specified correctly \n");
	printf("PI = \n"
		   // 字段宽度小于实际
		   "[[%7.2f]] \t\t(precision less than actual) \n"
		   // 字段宽度大于实际
		   "[[%7.8f]] \t\t(precision greater than actual) \n"
		   // 未指定字段宽度
		   "[[%7.f]] \t\t(no precision specified) \n\n",
		   PI, PI, PI);

	// 打印科学记数法
	printf("PRINTING SCIENTIFIC NOTATION \n\n");
	printf("income = \n"
		   // 宽度大，精度小
		   "[[%18.2e]] \t(field width large, precision small) \n"
		   // 宽度和精度中等大小
		   "[[%8.5e]] \t(field width and precision medium size) \n"
		   // 宽度和精度小
		   "[[%4.1e]] \t\t(field width and precision small) \n"
		   // 不指定
		   "[[%e]]    \t(no specifications) \n\n",
		   income, income, income, income);

	// 在转换规范中使用标志
	printf("USING A FLAG IN CONVERSION SPECIFICATIONS \n\n");
	printf("Days in year = \n"
		   // 字段宽度大，包含标识，标识 - 左对齐
		   "[[%-9d]] \t\t(field width large, flag included)\n",
		   DAYS_IN_YEAR);

	// flag = +，右对齐，如果显示的是一个正数，前面会有一个加号
	printf("[[%+5d]]\n", 1);
	printf("[[%+5d]]\n", -1);

	// flag = 0，使用0补齐
	printf("[[%05d]]\n", 1);


}
